package com.fth.learn.chapter06;

import java.util.Arrays;

/**
 * 写一个独立代码
 * YiMi
 *
 * @author YiMi
 * @version 1.0
 * @date 2023/7/14 9:13
 */

public class QueueForMe {

        //底层数组
         int[] queueArray;
         int index=0;
         int default_capacity = 10;
         int max_capacity = Integer.MAX_VALUE;

    /**
     * 数组想让人使用的时候要先开辟空间
     */

    public  QueueForMe(){
        queueArray = new int[default_capacity];
    }
    public boolean addend(int x){
        computeNewLength();
        queueArray[index++] = x;
        return true;
    }
    public void computeNewLength(){
        //如果相等 扩容1.5
        long newqueueArray = queueArray.length+(queueArray.length >> 1);
        int lastCapacity = newqueueArray > max_capacity ? max_capacity :(int)newqueueArray;
        if(queueArray.length==index){
            queueArray = Arrays.copyOf(queueArray,lastCapacity);
        }
    }
     public int getHeader(){
         // 容错  当index=0 ......
//        jdk提供的一个数组拷贝方法
         int t = queueArray[0];
         if(index != 1){
             System.arraycopy(queueArray, 1, queueArray, 0, index - 1);
         }
         index--;
         return t;
     }
     
//     public static int a(int x){
//        return 1;
//     }

    public static void main(String[] args) {
        QueueForMe q = new QueueForMe();
        QueueForMe q1 = new QueueForMe();
        q.addend(1);
        q.addend(2);
        q.addend(3);
        q.addend(4);

//        int a = a(3);
        System.out.println(q.getHeader());
        System.out.println(q.getHeader());
    }
}
